package org.jeecg.modules.dmyweb.data.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.dmyweb.home.service.IHomeService;
import org.jeecg.modules.util.RedisUtils;
import org.jeecg.modules.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags="dmyweb-数据")
@RestController
@RequestMapping("/dmyweb/data")
@Slf4j
public class
DataController {
    @Autowired
    private IHomeService homeService;
    @Autowired
    private RedisUtils redisUtils;
    /**
     * 订单数量折线图 /dmyweb/data/getOrderQtyLine
     * get请求
     * stockName 关联股东
     * staffName 关联提成员工
     * beginTime 开始时间 年月日
     * endTime 结束时间 年月日
     * productName 商品名称
     * patternPayment 付款方式  传文本
     * orderStatus 01 待付款 02 已付款 03 未自提 04  已自提 05  已完成  06  退款中 07  已退款
     * 所属门店去掉 这个在首页切换
     *
     *
     * 返回参数
     * dataX 时间轴
     * dataY1 //订单折线图数据
     * dataY2 //店铺会员折线图数据
     * dataY3 //股东会员折线图数据
     * dataY4 //股东折线图数据
     * dataY5 //营业额折线图数据
     * dataY6 //带动营业额折线图数据
     * dataY7 //员工销售提成折线图数据
     * dataY8 //员工服务提成折线图数据
     * dataY9 //股东分红折线图数据
     * dataY10 //股东管家分红折线图折线图数据
     *
     */
    @AutoLog(value = "数据-订单数量折线图")
    @ApiOperation(value = "数据-订单数量折线图", notes = "数据-订单数量折线图")
    @GetMapping(value = "/getOrderQtyLine")
    public Result<?> getOrderQtyLine(@RequestParam(name = "stockName", defaultValue = "") String stockName,
                                     @RequestParam(name = "staffName", defaultValue = "") String staffName,
                                     @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                     @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                     @RequestParam(name = "productName", defaultValue = "") String productName,
                                     @RequestParam(name = "patternPayment", defaultValue = "") String patternPayment,
                                     @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus,
                                     HttpServletRequest req){

        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            return Result.error("请先到首页选择店铺");
        }
        List<Map<String,Object>> listMap = homeService.getOrderQtyLine(stockName,staffName,beginTime,endTime,productName,patternPayment,orderStatus,storeId);
        int index = 0;
        int size = listMap.size();
        if(listMap.size()>30){
            index = listMap.size() - 30;
        }

        Map<String,Object> mapv = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<String> dataY1 = new ArrayList<>();
        List<String> dataY2 = new ArrayList<>();
        List<String> dataY3 = new ArrayList<>();
        List<String> dataY4 = new ArrayList<>();
        List<String> dataY5 = new ArrayList<>();
        List<String> dataY6 = new ArrayList<>();
        List<String> dataY7 = new ArrayList<>();
        List<String> dataY8 = new ArrayList<>();
        List<String> dataY9 = new ArrayList<>();
        List<String> dataY10 = new ArrayList<>();
        for(int i = index;i<size;i++){
            Map<String,Object>map = listMap.get(i);
            String times = ToolsUtils.getString(map.get("times"));
            if(StringUtils.isNotEmpty(times)){
                dataX.add(times);
                dataY1.add(String.valueOf(map.get("orderNum")));
                dataY2.add(String.valueOf(map.get("memberType1")));
                dataY3.add(String.valueOf(map.get("memberType2")));
                dataY4.add(String.valueOf(map.get("smNum")));
                dataY5.add(String.valueOf(map.get("saleroom")));
                dataY6.add(String.valueOf(map.get("profit")));
                dataY7.add(String.valueOf(map.get("sellMoney")));
                dataY8.add(String.valueOf(map.get("serveMoney")));
                dataY9.add(String.valueOf(map.get("stockBonus")));
                dataY10.add(String.valueOf(map.get("staffBonus")));
            }
        }

        mapv.put("dataX",dataX);
        mapv.put("dataY1",dataY1);//订单
        mapv.put("dataY2",dataY2);//店铺会员
        mapv.put("dataY3",dataY3);//股东会员
        mapv.put("dataY4",dataY4);//股东
        mapv.put("dataY5",dataY5);//营业额
        mapv.put("dataY6",dataY6);//带动营业额
        mapv.put("dataY7",dataY7);//员工销售提成
        mapv.put("dataY8",dataY8);//员工服务提成
        mapv.put("dataY9",dataY9);//股东分红
        mapv.put("dataY10",dataY10);//股东管家分红折线图
        return Result.OK(mapv);
    }

    /**
     * 店铺会员数折线图
     * dataX
     * data
     */
    @AutoLog(value = "数据-店铺会员数折线图")
    @ApiOperation(value = "数据-店铺会员数折线图", notes = "数据-店铺会员数折线图")
    @GetMapping(value = "/getStoreMemberQtyLine")
    public Result<?> getStoreMemberQtyLine(@RequestParam(name = "stockName", defaultValue = "") String stockName,
                                           @RequestParam(name = "staffName", defaultValue = "") String staffName,
                                           @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                           @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                           @RequestParam(name = "productName", defaultValue = "") String productName,
                                           @RequestParam(name = "patternPayment", defaultValue = "") String patternPayment,
                                           @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus){
        Map<String,Object> map = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<String> data = new ArrayList<>();
        for(int i=0;i<7;i++){
            String day = ToolsUtils.getLastDay(-(7-i));
            dataX.add(day);
            data.add("0");
        }
        map.put("dataX",dataX);
        map.put("data",data);
        return Result.OK(map);
    }

    /**
     * 股东会员数折线图
     * dataX
     * data
     */
    @AutoLog(value = "数据-股东会员数折线图")
    @ApiOperation(value = "数据-股东会员数折线图", notes = "数据-股东会员数折线图")
    @GetMapping(value = "/getStockMemberQtyLine")
    public Result<?> getStockMemberQtyLine(@RequestParam(name = "stockName", defaultValue = "") String stockName,
                                           @RequestParam(name = "staffName", defaultValue = "") String staffName,
                                           @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                           @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                           @RequestParam(name = "productName", defaultValue = "") String productName,
                                           @RequestParam(name = "patternPayment", defaultValue = "") String patternPayment,
                                           @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus){
        Map<String,Object> map = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<String> data = new ArrayList<>();
        for(int i=0;i<7;i++){
            String day = ToolsUtils.getLastDay(-(7-i));
            dataX.add(day);
            data.add("0");
        }
        map.put("dataX",dataX);
        map.put("data",data);
        return Result.OK(map);
    }

    /**
     * 股东数折线图
     * dataX
     * data
     */
    @AutoLog(value = "数据-股东数折线图")
    @ApiOperation(value = "数据-股东数折线图", notes = "数据-股东数折线图")
    @GetMapping(value = "/getStockQtyLine")
    public Result<?> getStockQtyLine(@RequestParam(name = "stockName", defaultValue = "") String stockName,
                                     @RequestParam(name = "staffName", defaultValue = "") String staffName,
                                     @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                     @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                     @RequestParam(name = "productName", defaultValue = "") String productName,
                                     @RequestParam(name = "patternPayment", defaultValue = "") String patternPayment,
                                     @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus){
        Map<String,Object> map = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<String> data = new ArrayList<>();
        for(int i=0;i<7;i++){
            String day = ToolsUtils.getLastDay(-(7-i));
            dataX.add(day);
            data.add("0");
        }
        map.put("dataX",dataX);
        map.put("data",data);
        return Result.OK(map);
    }

    /**
     * 营业额折线图
     * dataX
     * data
     */
    @AutoLog(value = "数据-营业额折线图")
    @ApiOperation(value = "数据-营业额折线图", notes = "数据-营业额折线图")
    @GetMapping(value = "/getTurnoverLine")
    public Result<?> getTurnoverLine(@RequestParam(name = "stockName", defaultValue = "") String stockName,
                                     @RequestParam(name = "staffName", defaultValue = "") String staffName,
                                     @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                     @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                     @RequestParam(name = "productName", defaultValue = "") String productName,
                                     @RequestParam(name = "patternPayment", defaultValue = "") String patternPayment,
                                     @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus){
        Map<String,Object> map = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<String> data = new ArrayList<>();
        for(int i=0;i<7;i++){
            String day = ToolsUtils.getLastDay(-(7-i));
            dataX.add(day);
            data.add("0");
        }
        map.put("dataX",dataX);
        map.put("data",data);
        return Result.OK(map);
    }

    /**
     * 带动营业额折线图
     * dataX
     * data
     */
    @AutoLog(value = "数据-带动营业额折线图")
    @ApiOperation(value = "数据-带动营业额折线图", notes = "数据-带动营业额折线图")
    @GetMapping(value = "/getDriveTurnoverLine")
    public Result<?> getDriveTurnoverLine(@RequestParam(name = "stockName", defaultValue = "") String stockName,
                                          @RequestParam(name = "staffName", defaultValue = "") String staffName,
                                          @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                          @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                          @RequestParam(name = "productName", defaultValue = "") String productName,
                                          @RequestParam(name = "patternPayment", defaultValue = "") String patternPayment,
                                          @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus){
        Map<String,Object> map = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<String> data = new ArrayList<>();
        for(int i=0;i<7;i++){
            String day = ToolsUtils.getLastDay(-(7-i));
            dataX.add(day);
            data.add("0");
        }
        map.put("dataX",dataX);
        map.put("data",data);
        return Result.OK(map);
    }

    /**
     * 员工销售提成折线图
     * dataX
     * data
     */
    @AutoLog(value = "数据-员工销售提成折线图")
    @ApiOperation(value = "数据-员工销售提成折线图", notes = "数据-员工销售提成折线图")
    @GetMapping(value = "/getStaffSalesCommissionLine")
    public Result<?> getStaffSalesCommissionLine(@RequestParam(name = "stockName", defaultValue = "") String stockName,
                                                 @RequestParam(name = "staffName", defaultValue = "") String staffName,
                                                 @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                                 @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                                 @RequestParam(name = "productName", defaultValue = "") String productName,
                                                 @RequestParam(name = "patternPayment", defaultValue = "") String patternPayment,
                                                 @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus){
        Map<String,Object> map = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<String> data = new ArrayList<>();
        for(int i=0;i<7;i++){
            String day = ToolsUtils.getLastDay(-(7-i));
            dataX.add(day);
            data.add("0");
        }
        map.put("dataX",dataX);
        map.put("data",data);
        return Result.OK(map);
    }

    /**
     * 员工服务提成折线图
     * dataX
     * data
     */
    @AutoLog(value = "数据-员工服务提成折线图")
    @ApiOperation(value = "数据-员工服务提成折线图", notes = "数据-员工服务提成折线图")
    @GetMapping(value = "/getStaffServiceCommissionLine")
    public Result<?> getStaffServiceCommissionLine(@RequestParam(name = "stockName", defaultValue = "") String stockName,
                                                   @RequestParam(name = "staffName", defaultValue = "") String staffName,
                                                   @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                                   @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                                   @RequestParam(name = "productName", defaultValue = "") String productName,
                                                   @RequestParam(name = "patternPayment", defaultValue = "") String patternPayment,
                                                   @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus){
        Map<String,Object> map = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<String> data = new ArrayList<>();
        for(int i=0;i<7;i++){
            String day = ToolsUtils.getLastDay(-(7-i));
            dataX.add(day);
            data.add("0");
        }
        map.put("dataX",dataX);
        map.put("data",data);
        return Result.OK(map);
    }

    /**
     * 股东分红折线图
     * dataX
     * data
     */
    @AutoLog(value = "数据-股东分红折线图")
    @ApiOperation(value = "数据-股东分红折线图", notes = "数据-股东分红折线图")
    @GetMapping(value = "/getStockBonusLine")
    public Result<?> getStockBonusLine(@RequestParam(name = "stockName", defaultValue = "") String stockName,
                                       @RequestParam(name = "staffName", defaultValue = "") String staffName,
                                       @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                       @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                       @RequestParam(name = "productName", defaultValue = "") String productName,
                                       @RequestParam(name = "patternPayment", defaultValue = "") String patternPayment,
                                       @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus){
        Map<String,Object> map = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<String> data = new ArrayList<>();
        for(int i=0;i<7;i++){
            String day = ToolsUtils.getLastDay(-(7-i));
            dataX.add(day);
            data.add("0");
        }
        map.put("dataX",dataX);
        map.put("data",data);
        return Result.OK(map);
    }

    /**
     * 股东管家分红折线图
     * dataX
     * data
     */
    @AutoLog(value = "数据-股东管家分红折线图")
    @ApiOperation(value = "数据-股东管家分红折线图", notes = "数据-股东管家分红折线图")
    @GetMapping(value = "/getStockKeeperBonusLine")
    public Result<?> getStockKeeperBonusLine(@RequestParam(name = "stockName", defaultValue = "") String stockName,
                                             @RequestParam(name = "staffName", defaultValue = "") String staffName,
                                             @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                             @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                             @RequestParam(name = "productName", defaultValue = "") String productName,
                                             @RequestParam(name = "patternPayment", defaultValue = "") String patternPayment,
                                             @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus){
        Map<String,Object> map = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<String> data = new ArrayList<>();
        for(int i=0;i<7;i++){
            String day = ToolsUtils.getLastDay(-(7-i));
            dataX.add(day);
            data.add("0");
        }
        map.put("dataX",dataX);
        map.put("data",data);
        return Result.OK(map);
    }
}
